import type { VercelRequest } from '@vercel/node';
import {verifyJwtToken} from "../../util/jwt"
import { UnLoginError } from '../../enums/ErrorsEnum';

/**
 * 检查是否登录
 * @returns 用户名
 * @throws UnLoginError 未登录
 * @throws ExpiredTokenError 登录凭证已过期
 * @throws InvalidTokenError 登录凭证无效
 */
export function checkLogin(request: VercelRequest) : string {
    const token = request.headers.authorization;
    if (token) {
        return verifyJwtToken(token)
    } else throw new UnLoginError("请登录后继续操作");
}